package com.sinodata.bsm.center.service.performance;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.RealTimeViewConfCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.expression.BatchCalctor;
import com.sinodata.bsm.center.message.ProbeMessenger;
import com.sinodata.bsm.common.vo.RealTimeViewConf;
import com.sinodata.bsm.common.vo.TopoTooltipConf;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author hehaiyang_itsm
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-17 下午5:16:07          hehaiyang_itsm        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class RealTimeService {

    @Autowired
    private BaseDao baseDao;
    @Autowired
    RealTimeViewConfCache realTimeViewConfCache;
    @Autowired
    ResCache resCache;

    /**
     * 获得资源类别的性能图定义
     * @return
     */
    public List<RealTimeViewConf> findAllTypeView() {
        List<RealTimeViewConf> confis = new ArrayList<RealTimeViewConf>();
        for (RealTimeViewConf conf : realTimeViewConfCache.getAll()) {
            if (conf.getResTypeId() != null && conf.getXml() != null) {
                confis.add(conf);
            }
        }
        Collections.sort(confis, new TimeViewConfCompare());
        return confis;

    }

    /**
     * 获得资源类别的性能图定义
     * @return
     */
    public List<RealTimeViewConf> findAllFreeCustomView() {
        List<RealTimeViewConf> confis = new ArrayList<RealTimeViewConf>();
        for (RealTimeViewConf conf : realTimeViewConfCache.getAll()) {
            if (conf.getResTypeId() == null && conf.getXml() != null) {
                confis.add(conf);
            }
        }
        Collections.sort(confis, new TimeViewConfCompare());
        return confis;
    }

    class TimeViewConfCompare implements Comparator<RealTimeViewConf> {
        @Override
        public int compare(RealTimeViewConf o1, RealTimeViewConf o2) {
            long modifyTimeOne = o1.getModifyTime() == null ? -1 : o1.getModifyTime().getTime();
            long modifyTimeTwo = o2.getModifyTime() == null ? -1 : o2.getModifyTime().getTime();
            int compareValue = modifyTimeOne > modifyTimeTwo ? -1 : 1;
            return compareValue;
        }

    }

    /**
     * <p>通过资源ID 找到实时性能图配置记录<p>
     * @param resId
     * @return
     */
    public RealTimeViewConf findRealTimeView(Long resId) {
        //        baseDao.findObjectByHql(RealTimeViewConf.class, "from RealTimeViewConf t where t.resId");
        //        List confList = baseDao.findByNamedQuery("RealTimeViewConf.findByResId", resId);
        //        if(confList.size()==0){
        //            Long resTypeId =0l;
        //            confList = baseDao.findByNamedQuery("RealTimeViewConf.findByResTypeId", resTypeId); 
        //        }
        Long resTypeId = resCache.get(resId).getResTypeId();
        Collection<RealTimeViewConf> confs = realTimeViewConfCache.getAll();
        RealTimeViewConf realTimeViewConf = null;
        for (RealTimeViewConf conf : confs) {
            if ((resTypeId.equals(conf.getResTypeId()) && conf.getXml() != null)) {
                realTimeViewConf = conf;
            }
        }
        return realTimeViewConf;
    }

    public RealTimeViewConf delRealTimeViewConf(Long id) {
        RealTimeViewConf conf = realTimeViewConfCache.get(id);
        if (conf != null) {
            baseDao.delete(conf);
            realTimeViewConfCache.remove(id);
        }
        return conf;
    }

    public String queryRealData(String inStr) {
        BatchCalctor bc = new BatchCalctor();
        String ret = bc.calcXml(inStr);
        return ret;
    }

    public RealTimeViewConf findByResTypeId(Long resTypeId) {
        Collection<RealTimeViewConf> confs = realTimeViewConfCache.getAll();
        RealTimeViewConf realTimeViewConf = null;
        for (RealTimeViewConf conf : confs) {
            if (resTypeId.equals(conf.getResTypeId())) {
                realTimeViewConf = conf;
            }
        }
        return realTimeViewConf;
    }

    public TopoTooltipConf findTooltipByResTypeId(Long resTypeId) {
        Collection<TopoTooltipConf> confs = realTimeViewConfCache.getAllTooltip();
        TopoTooltipConf tipConf = null;
        for (TopoTooltipConf conf : confs) {
            if (resTypeId.equals(conf.getResTypeId())) {
                tipConf = conf;
            }
        }
        return tipConf;
    }

    /**
     * 获取一个资源类别对应的综合性能页面
     * @param typeId 资源类别id
     * @return 页面对应的字符串
     */
    public String findPageByResType(Long typeId) {
        Collection<RealTimeViewConf> confs = realTimeViewConfCache.getAll();
        for (RealTimeViewConf conf : confs) {
            if (typeId.equals(conf.getResTypeId()) && conf.getPage() != null && conf.getPage().length() > 0) {
                return conf.getPage();
            }
        }
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public RealTimeViewConf saveRealTimeViewConf(RealTimeViewConf entity) {
        baseDao.saveOrUpdate(entity);
        realTimeViewConfCache.put(entity.getId(), entity);
        return entity;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public TopoTooltipConf saveTooltipConf(TopoTooltipConf entity) {
        baseDao.saveOrUpdate(entity);
        realTimeViewConfCache.putTooltip(entity.getId(), entity);
        return entity;
    }

    public void collectPropertyValues(Long resIds[]) {
        ProbeMessenger.notifyCollect(resIds);
    }
}
